﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Kooboo.CMS.Content.Models;
using Kooboo.CMS.Content.Query;
using Ionic.Zip;
using System.IO;

namespace Kooboo.CMS.DataModule.Services
{
    public class ExportService
    {
        public byte[] ExportAsZip(Repository repository, string folderName)
        {
            var textFolder = new TextFolder(repository, folderName);
            var contents = textFolder.CreateQuery().ToArray();


            MemoryStream ms = new MemoryStream();
            Kooboo.Runtime.Serialization.DataContractSerializationHelper.Serialize(contents, ms);
            ms.Position = 0;

            MemoryStream outputStream = new MemoryStream();


            ZipFile zipFile = new ZipFile();
            zipFile.AddEntry(folderName + ".xml", ms);
            zipFile.Save(outputStream);
            outputStream.Position = 0;

            byte[] data = new byte[outputStream.Length];
            outputStream.Read(data, 0, data.Length);


            return data;
        }
    }
}